//
//  SlideCollectionViewCell.m
//  CollectionViewSlideDelete
//
//  Created by 赵忠鹏 on 2018/5/16.
//  Copyright © 2018年 zhaozhongpeng. All rights reserved.
//

#import "SlideCollectionViewCell.h"


@interface SlideCollectionViewCell ()



@property (nonatomic,strong) UIButton *deleteButton;

@end

@implementation SlideCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
        
        [self.contentView addSubview:self.mainLabal];
        self.mainLabal.frame = CGRectMake(100, 0, 200, 30);
        
        
    }
    return self;
}

- (void)createUI{
    UISwipeGestureRecognizer * LeftRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [LeftRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.contentView addGestureRecognizer:LeftRecognizer];
   
    UISwipeGestureRecognizer * RightRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [RightRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];
    [self.contentView addGestureRecognizer:RightRecognizer];
    
    [self addSubview:self.deleteButton];
    self.deleteButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 50, 0, 50, 50);
    [self sendSubviewToBack:self.deleteButton];
    
    
    UIView *bgView = [[UIView alloc] initWithFrame:self.contentView.bounds];
    bgView.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
    [self.contentView addSubview:bgView];
    
}

-(UIButton *)deleteButton{
    if (_deleteButton == nil) {
        _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleteButton addTarget:self action:@selector(deleteButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_deleteButton setTitle:@"删除" forState:UIControlStateNormal];
        _deleteButton.backgroundColor = [UIColor redColor];
    }
    return _deleteButton;
}

-(void)deleteButtonClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(deleteButtonClick:) ]) {
        [self.delegate deleteButtonClick:self];
    }
}


- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    [UIView animateWithDuration:0.3 animations:^{
        if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
            if ([self.delegate respondsToSelector:@selector(swipeGestureLeft:)]) {
                [self.delegate swipeGestureLeft:self];
            }
            
            self.contentView.frame = CGRectMake(-50, 0, [UIScreen mainScreen].bounds.size.width, 50);
        }else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight){
            [self reset];
        }
    }];
}

- (void)reset{
    [UIView animateWithDuration:0.3 animations:^{
        self.contentView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 50);
    }];
}


- (UILabel *)mainLabal{
    if (_mainLabal == nil) {
        _mainLabal = [[UILabel alloc] init];
        _mainLabal.textAlignment = NSTextAlignmentCenter;
    }
    return _mainLabal;
}


@end
